package drawing 
{
	import app.textures.TextureInfo;
	import app.textures.TextureInfoGroupe;
	import events.FurniturePalleteEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import furnitures.FurnitureModel;
	import utils.Logger;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	
	 /* When tabletop Palette changed
	 * @eventType events.FurniturePalleteEvent
	 */
	[Event(name = "TABLETOP_PALETTE_CHANGED", type = "events.FurniturePalleteEvent")]
	
	 /* When facade Palette changed
	 * @eventType events.FurniturePalleteEvent
	 */
	[Event(name = "FACADE_PALETTE_CHANGED", type = "events.FurniturePalleteEvent")]
	
	 /* When corpus Palette changed
	 * @eventType events.FurniturePalleteEvent
	 */
	[Event(name = "CORPUS_PALETTE_CHANGED", type = "events.FurniturePalleteEvent")]
	
	/**
	  * Dispatch palette inited
	  */
	[Event(name = "complete", type = "flash.events.Event")]
	
	public class FurniturePaletteProvider extends EventDispatcher
	{
		//private var _tabletopPalette:IPalette;
		//private var _facadePalette:IPalette;
		//private var _corpusPalette:IPalette;
		
		private var _tabletopPal:HybridPalette;
		private var _facadePal:HybridPalette;
		private var _corpusPalette:HybridPalette;
		
		private var _techPalette:HybridPalette;
		
		private var _counter:int;
		
		public function FurniturePaletteProvider() 
		{
			
		}
		
		public function setupPalette(textureGroupe:TextureInfoGroupe, textureInfo:TextureInfo):void
		{
			
			switch(textureGroupe.targetSurface)
			{
				case TextureInfoGroupe.TARGET_SIDE_FACADE:
						if (_facadePal == null)
						{
							_facadePal = new HybridPalette("facade");
						}
						_facadePal.setupTexture(textureGroupe.textureType, textureInfo);
					break;
				case TextureInfoGroupe.TARGET_SIDE_TABLETOP:
						if (_tabletopPal == null)
						{
							_tabletopPal = new HybridPalette("tabletop");
						}
						_tabletopPal.setupTexture(textureGroupe.textureType, textureInfo);
					break;
				case TextureInfoGroupe.TARGET_SIDE_CORPUS:
						if (_corpusPalette == null)
						{
							_corpusPalette = new HybridPalette("corpus");
						}
						_corpusPalette.setupTexture(textureGroupe.textureType, textureInfo);
					break;
				case TextureInfoGroupe.TARGET_SIDE_TECH:
						if (_techPalette == null)
						{
							_techPalette = new HybridPalette("tech");
						}
						_techPalette.setupTexture(textureGroupe.textureType, textureInfo);
					break;
			}
		}
		
		/*public function setupPalette(textureGroupe:TextureInfoGroupe, textureInfo:TextureInfo):void
		{
			switch(textureGroupe.targetSurface)
			{
				case TextureInfoGroupe.TARGET_SIDE_FACADE:
						facadePalette = PaletteManager.getInstance().getPalette(textureGroupe,textureInfo);
					break;
				case TextureInfoGroupe.TARGET_SIDE_TABLETOP:
						tabletopPalette = PaletteManager.getInstance().getPalette(textureGroupe,textureInfo);
					break;
				case TextureInfoGroupe.TARGET_SIDE_CORPUS:
						corpusPalette = PaletteManager.getInstance().getPalette(textureGroupe,textureInfo);
					break;
			}
		}*/
		
		public function prepareSizes(sizes:Vector.<String>):void
		{
			_counter = 0;
			if (_tabletopPal != null)
			{
				_tabletopPal.addEventListener(Event.COMPLETE, handlePalleteInited);
				_counter++;
			}
			
			if (_facadePal != null)
			{
				_facadePal.addEventListener(Event.COMPLETE, handlePalleteInited);
				_counter++;
			}
			
			if (_corpusPalette != null)
			{
				_corpusPalette.addEventListener(Event.COMPLETE, handlePalleteInited);
				_counter++;
			}
			
			if (_techPalette != null)
			{
				_techPalette.addEventListener(Event.COMPLETE, handlePalleteInited);
				_counter++;
			}
			
			if (_counter == 0)
			{
				log("palettes null - return");
				complete();
				return;
			}
			if (_tabletopPal != null)
			{
				_tabletopPal.setupSizes(sizes);
			}
			
			if (_facadePal != null)
			{
				_facadePal.setupSizes(sizes);
			}
			
			if (_corpusPalette != null)
			{
				_corpusPalette.setupSizes(sizes);
			}
			
			if (_techPalette != null)
			{
				_techPalette.setupSizes(sizes);
			}
			
			// _tabletopPal addlistener complete
			// _facadePal addlistener complete
			// after complete dispatch complete
			
		}
		
		private function handlePalleteInited(e:Event):void 
		{
			_counter--;
			if (_counter == 0)
			{
				if (_tabletopPal != null)
				{
					_tabletopPal.removeEventListener(Event.COMPLETE, handlePalleteInited);
				}
				if (_facadePal != null)
				{
					_facadePal.removeEventListener(Event.COMPLETE, handlePalleteInited);
				}
				if (_corpusPalette != null)
				{
					_corpusPalette.removeEventListener(Event.COMPLETE, handlePalleteInited);
				}
				if (_techPalette != null)
				{
					_techPalette.removeEventListener(Event.COMPLETE, handlePalleteInited);
				}
				complete();
			}
		}
		
		private function complete():void 
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		
		public function get tabletopPalette():IPalette 
		{
			//return _tabletopPalette;
			return _tabletopPal;
		}
		
		/*public function set tabletopPalette(value:IPalette):void 
		{
			_tabletopPalette = value;
			dispatchEvent(new FurniturePalleteEvent(FurniturePalleteEvent.TABLETOP_PALETTE_CHANGED));
		}*/
		
		public function get facadePalette():IPalette 
		{
			//return _facadePalette;
			return _facadePal;
		}
		
		/*public function set facadePalette(value:IPalette):void 
		{
			_facadePalette = value;
			dispatchEvent(new FurniturePalleteEvent(FurniturePalleteEvent.FACADE_PALETTE_CHANGED));
		}*/
		
		public function get corpusPalette():IPalette 
		{
			return _corpusPalette;
		}
		
		public function get techPalette():HybridPalette 
		{
			return _techPalette;
		}
		
		/*public function set corpusPalette(value:IPalette):void 
		{
			_corpusPalette = value;
			dispatchEvent(new FurniturePalleteEvent(FurniturePalleteEvent.CORPUS_PALETTE_CHANGED));
		}*/
		
		private function log(msg:String):void
		{
			Logger.log(this, msg);
		}
		
	}

}